#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
typedef pair<int, int> PII;
const int N = 10010;

int a[N]= {-1};
int st[N] = {-1};

int n;
int main(){
    cin >> n;
    for(int i = 1 ; i <= n; i++)
        scanf("%d", &a[i]);
    for(int i = n; i > 0; i --){
        if(a[i*2] ==1  ||a[i * 2 + 1] == 1 )
        {
            st[i] = 1;
        }
    }
    for(int i = 1; i <= n; i ++)
    {
        if(a[i] == 0 &&st[i] != 1)
            a[i] = -1;
    }

    queue<PII> q;
    q.push({a[1],1});
    while(!q.empty())
    {
        PII t = q.front();
        q.pop();
        if(t.first  == -1)
        {
            cout << "-1"<<" ";
            continue;
        }
        cout << t.first << " ";
    
        q.push({a[t.second * 2], t.second*2});
        q.push({a[t.second * 2 + 1], t.second* 2 + 1});

    }
    // for(int i = 0; i < n; i ++)
    // {
    //     cout << a[i] << " " ;
    // }
    return 0;
}